/*
 * DA2.c
 *
 * Created: 2/27/2014 1:04:08 PM
 *  Author: Omar Salazar
 */ 

#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 1000000UL	//1 MHz
#include <util/delay.h>


volatile int global_inter;				//global interrupt value
ISR (INT0_vect)
{
	cli();								//disables interrupt
	if(global_inter == 2)				//catch the 2nd falling edge to turn on PC1
	{
		PORTC |= (1 << PORTC1);			// turn on PC1 which corresponds to the 10th LED
	}
	if(global_inter == 4)				//catch the 4th falling edge to toggle off PC1
	{
		PORTC &= ~(1 << PORTC1);		//turn off PC1
		global_inter = 0;				//reset the interrupt counter
	}
	sei();								//enables the interrupt value
}

int main(void)
{
	int count = 0;						//keeps track of the counter for PINB
	int count_ten = 0;					//keeps track of the 10 cycles
	DDRB = 0xFF;						//Set PORTB for output
	DDRC |= (1<<PORTC0)|(1<<PORTC1);	//Set PC0 and PC1 for ouput  (can also be set like this: DDRC=0x05
	PORTB = count;						//Have the counter start at 0
	EICRA |= (1<<ISC01);				//the falling edge of INT0 generates an interrupt request
	EIMSK |= (1<<INT0);					//Enable external interrupt 0
	sei();								//enable interrupts
	while(1)
    {
        _delay_ms(250);					//rising edge
		count++;						//increment global counter
		PORTB = count;					//output the updated counter
		count_ten++;					//increment the counter to keep track of ten pulses
		if(count_ten==10)				//time for the 9th LED to turn on
		{
			PORTC |= (1 << PORTC0);     //turn on PC0, PC0 will be connected to PD2 for INT0
		}
		if(count_ten == 20)				//time for the 9th LED to shut off
		{
			global_inter++;				//increment global counter
			PORTC &= ~(1 << PORTC0);    //turn off PC0,  PC0 will be connected to PD2 for INT0
			count_ten = 0;				//reset counter
		}
		_delay_ms(250);					//falling edge
	}
}
